package com.hd.trans.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.databinding.DataBindingUtil
import com.hd.trans.R
import com.hd.trans.binding.visible
import com.hd.trans.databinding.LayoutVoicePlayViewBinding
import com.hd.trans.ui.base.TransInit

class VoicePlayView: FrameLayout {
    private var isLoading: Boolean = false
    private var isPlaying: Boolean = false
    private var animator: AnimationDrawable? = null
    private val mDataBing: LayoutVoicePlayViewBinding =
        DataBindingUtil.inflate(LayoutInflater.from(context),
            R.layout.layout_voice_play_view,
            this,
            true)

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    @SuppressLint("UseCompatLoadingForDrawables", "ResourceAsColor")
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context,
        attrs,
        defStyleAttr){
        val typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.VoicePlayView)
        val animationDrawable =
            typedArray.getResourceId(R.styleable.VoicePlayView_animationDrawable, 0)
        val normalDrawable =
            typedArray.getResourceId(R.styleable.VoicePlayView_normalDrawable, 0)
        val loadDrawable =
            typedArray.getDrawable(R.styleable.VoicePlayView_loadDrawable)
        val tintColor =
            typedArray.getColor(R.styleable.VoicePlayView_tintColor, 0)
        typedArray.recycle()
        mDataBing.apply {
            animator = if(animationDrawable != 0){
                context.getDrawable(animationDrawable) as AnimationDrawable
            }else{
                context.getDrawable(R.drawable.trans_voice_play_gif) as AnimationDrawable
            }
            statusPlaying.setImageDrawable(animator)
            if(tintColor != 0){
                statusPlaying.imageTintList = ColorStateList.valueOf(tintColor)
                statusNormal.imageTintList = ColorStateList.valueOf(tintColor)
                statusLoading.indeterminateTintList = ColorStateList.valueOf(tintColor)
            }
            if(normalDrawable != 0){
                statusNormal.setImageResource(normalDrawable)
            }
            if(loadDrawable != null){
                statusLoading.indeterminateDrawable = loadDrawable
            }
        }
    }

    /**
     * 开始加载(语音下载中)
     */
    fun loading(){
        isLoading = true
        mDataBing.statusLoading.visible = true
        mDataBing.statusNormal.visible = false
        mDataBing.statusPlaying.visible = false
        isEnabled = false
    }

    /**
     * 加载失败(语音下载中)
     */
    fun loadingFailed() {
        loadingComplete()
    }

    /**
     * 加载完成(语音下载完成)
     */
    fun loadingComplete() {
        isLoading = false
        mDataBing.statusLoading.visible = false
        mDataBing.statusNormal.visible = true
        mDataBing.statusPlaying.visible = false
        isEnabled = true
    }

    fun setIsNormal(isNormal: Boolean, isReverse: Boolean) {
    }

    /**
     * 开始播放
     */
    fun startPlay() {
        loadingComplete()
        isPlaying = true
        mDataBing.statusNormal.visible = false
        mDataBing.statusPlaying.visible = true
        animator?.start()
    }


    /**
     * 停止播放
     */
    fun stopPlay() {
        loadingComplete()
        isPlaying = false
        animator?.stop()
    }

    fun isPlaying(): Boolean {
        return isPlaying
    }

    fun isLoading(): Boolean {
        return isLoading
    }
}